package com.cet.spring_batch_study.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.batch.core.*;
import org.springframework.batch.core.configuration.JobRegistry;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
import org.springframework.batch.core.repository.JobRestartException;
import org.springframework.web.bind.annotation.*;

/**
 * @author NSOC
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/job/study")
public class JobLaunchController {

    private final Job endOfDay;

    private final Job fileDeletingJob;

    private final Job cooking;

    private final Job playInfoIngestingJob;

    private final Job xmlParseJob;

    private final Job dbDataIngestingJob;

    private final Job reconJob;

    private final JobLauncher jobLauncher;

    private final JobRegistry jobRegistry;

    @PostMapping("/launch/{times}")
    public Object launchEndOfDayJob(@PathVariable("times") Long times) throws JobInstanceAlreadyCompleteException, JobExecutionAlreadyRunningException, JobParametersInvalidException, JobRestartException {
        System.out.println("web端传入的参数times是" + times);
        JobParameters jobParameters = new JobParametersBuilder()
                .addLong("loop.times", times)
                .toJobParameters();
        JobExecution jobExecution = jobLauncher.run(endOfDay, jobParameters);
        return jobExecution.getExitStatus();
    }

    @GetMapping("/registry/job/names")
    public void practiceJobRegistry() {
        System.out.println(jobRegistry.getJobNames());

    }

    @PostMapping("/launch/files/delete")
    public Object launchFileDeletingJob(@RequestParam("dirPath") String dirPath) throws JobInstanceAlreadyCompleteException, JobExecutionAlreadyRunningException, JobParametersInvalidException, JobRestartException {
        JobParameters jobParameters = new JobParametersBuilder()
                .addString("dir.path", dirPath)
                .toJobParameters();
        JobExecution jobExecution = jobLauncher.run(fileDeletingJob, jobParameters);
        return jobExecution.getExitStatus();
    }

    @PostMapping("/launch/cookingFoods")
    public Object cookingFoods() throws JobInstanceAlreadyCompleteException, JobExecutionAlreadyRunningException, JobParametersInvalidException, JobRestartException {
        JobParameters jobParameters = new JobParameters();
        JobExecution jobExecution = jobLauncher.run(cooking, jobParameters);
        return jobExecution.getExitStatus();
    }

    @PostMapping("/launch/player/ingesting")
    public Object ingestingPlayerInfo() throws JobInstanceAlreadyCompleteException, JobExecutionAlreadyRunningException, JobParametersInvalidException, JobRestartException {
        JobExecution jobExecution = jobLauncher.run(playInfoIngestingJob, new JobParameters());
        return jobExecution.getExitStatus();
    }

    @PostMapping("/launch/xml/parse")
    public Object xmlParse() throws JobInstanceAlreadyCompleteException, JobExecutionAlreadyRunningException, JobParametersInvalidException, JobRestartException {
        JobExecution jobExecution = jobLauncher.run(xmlParseJob, new JobParameters());
        return jobExecution.getExitStatus();
    }

    @PostMapping("/launch/db/credit/ingesting")
    public Object dbDataIngesting() throws JobInstanceAlreadyCompleteException, JobExecutionAlreadyRunningException, JobParametersInvalidException, JobRestartException {
        JobExecution jobExecution = jobLauncher.run(dbDataIngestingJob, new JobParameters());
        return jobExecution.getExitStatus();
    }

    @PostMapping("/launch/recon")
    public Object recon() throws JobInstanceAlreadyCompleteException, JobExecutionAlreadyRunningException, JobParametersInvalidException, JobRestartException {
        JobParametersBuilder jobParametersBuilder = new JobParametersBuilder();
        jobParametersBuilder.addString("run.date", "2025-11-02");
        jobParametersBuilder.addLong("time", System.currentTimeMillis());
        JobExecution jobExecution = jobLauncher.run(reconJob, jobParametersBuilder.toJobParameters());
        return jobExecution.getExitStatus();
    }
}
